/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFPrincipalTecnico.java
 *
 * Created on 10/02/2012, 19:51:32
 */
package view.tecnico;

import control.Sessao;
import view.admin.JDConfiguracao;
import java.beans.PropertyVetoException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import model.Pessoa;
import util.StatusBar;
import util.Versao;
import view.JIFSimpleFactory;


/**
 *
 * @author Felipe
 */
public class JFPrincipalTecnico extends JFrame {
    
    private Pessoa usuarioLogado;
    private StatusBar statusBar;
     
    /** Creates new form JFPrincipalTecnico */
    public JFPrincipalTecnico() {
        this.usuarioLogado = Sessao.getInstance().getUsuarioLogado();
        initComponents();
        statusBar = new StatusBar();
        this.status.add(statusBar);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtbPrincipal = new javax.swing.JToolBar();
        btnControleChamado = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        pnlCentro = new javax.swing.JPanel();
        pnlCentroLeste = new javax.swing.JPanel();
        pnlCentroOeste = new javax.swing.JPanel();
        pnlCentroSul = new javax.swing.JPanel();
        status = new javax.swing.JPanel();
        pnlCentroNorte = new javax.swing.JPanel();
        jDesktopPane = new javax.swing.JDesktopPane();
        jmbPrincipal = new javax.swing.JMenuBar();
        jmSistema = new javax.swing.JMenu();
        jmiSair = new javax.swing.JMenuItem();
        jmChamado = new javax.swing.JMenu();
        jmiControleChamado = new javax.swing.JMenuItem();
        jmAjuda = new javax.swing.JMenu();
        jmiSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(Versao.TITULO);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jtbPrincipal.setRollover(true);

        btnControleChamado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/atendente3.png"))); // NOI18N
        btnControleChamado.setToolTipText("Controle de chamado");
        btnControleChamado.setFocusable(false);
        btnControleChamado.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnControleChamado.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnControleChamado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnControleChamadoActionPerformed(evt);
            }
        });
        jtbPrincipal.add(btnControleChamado);

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i48x48/sair.png"))); // NOI18N
        btnSair.setToolTipText("Sair do sistema");
        btnSair.setFocusable(false);
        btnSair.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSair.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jtbPrincipal.add(btnSair);

        getContentPane().add(jtbPrincipal, java.awt.BorderLayout.PAGE_START);

        pnlCentro.setLayout(new java.awt.BorderLayout());
        pnlCentro.add(pnlCentroLeste, java.awt.BorderLayout.EAST);
        pnlCentro.add(pnlCentroOeste, java.awt.BorderLayout.WEST);

        pnlCentroSul.setLayout(new java.awt.BorderLayout());

        status.setLayout(new java.awt.BorderLayout());
        pnlCentroSul.add(status, java.awt.BorderLayout.EAST);

        pnlCentro.add(pnlCentroSul, java.awt.BorderLayout.SOUTH);
        pnlCentro.add(pnlCentroNorte, java.awt.BorderLayout.NORTH);

        jDesktopPane.setBackground(new java.awt.Color(153, 153, 153));
        pnlCentro.add(jDesktopPane, java.awt.BorderLayout.CENTER);

        getContentPane().add(pnlCentro, java.awt.BorderLayout.CENTER);

        jmSistema.setText("Sistema");

        jmiSair.setText("Sair");
        jmiSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSairActionPerformed(evt);
            }
        });
        jmSistema.add(jmiSair);

        jmbPrincipal.add(jmSistema);

        jmChamado.setText("Chamado");

        jmiControleChamado.setText("Controle de chamados");
        jmiControleChamado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiControleChamadoActionPerformed(evt);
            }
        });
        jmChamado.add(jmiControleChamado);

        jmbPrincipal.add(jmChamado);

        jmAjuda.setText("Ajuda");

        jmiSobre.setText("Sobre");
        jmiSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSobreActionPerformed(evt);
            }
        });
        jmAjuda.add(jmiSobre);

        jmbPrincipal.add(jmAjuda);

        setJMenuBar(jmbPrincipal);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-900)/2, (screenSize.height-597)/2, 900, 597);
    }// </editor-fold>//GEN-END:initComponents

    private void jmiSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiSairActionPerformed
        fecharAplicacao();
    }//GEN-LAST:event_jmiSairActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        fecharAplicacao();
    }//GEN-LAST:event_btnSairActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        fecharAplicacao();
    }//GEN-LAST:event_formWindowClosing

    private void jmiControleChamadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiControleChamadoActionPerformed
        abrirJIFChamadoTecnico();
    }//GEN-LAST:event_jmiControleChamadoActionPerformed

    private void btnControleChamadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnControleChamadoActionPerformed
        abrirJIFChamadoTecnico();
    }//GEN-LAST:event_btnControleChamadoActionPerformed

    private void jmiSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiSobreActionPerformed
        JOptionPane.showMessageDialog(null,
                Versao.SOBRE,                
                Versao.SOBRE_VER,
                JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jmiSobreActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnControleChamado;
    private javax.swing.JButton btnSair;
    private javax.swing.JDesktopPane jDesktopPane;
    private javax.swing.JMenu jmAjuda;
    private javax.swing.JMenu jmChamado;
    private javax.swing.JMenu jmSistema;
    private javax.swing.JMenuBar jmbPrincipal;
    private javax.swing.JMenuItem jmiControleChamado;
    private javax.swing.JMenuItem jmiSair;
    private javax.swing.JMenuItem jmiSobre;
    private javax.swing.JToolBar jtbPrincipal;
    private javax.swing.JPanel pnlCentro;
    private javax.swing.JPanel pnlCentroLeste;
    private javax.swing.JPanel pnlCentroNorte;
    private javax.swing.JPanel pnlCentroOeste;
    private javax.swing.JPanel pnlCentroSul;
    private javax.swing.JPanel status;
    // End of variables declaration//GEN-END:variables

    private void abrirConfiguracao() {
        JDConfiguracao configuracao = new JDConfiguracao(this, true);	
    }
    
    private void addJIF(JInternalFrame jif) {
        jDesktopPane.add(jif);
        try {
            jif.setMaximum(rootPaneCheckingEnabled);
            jif.setVisible(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(JFPrincipalTecnico.class.getName()).log(Level.SEVERE, null, ex);
        }       
    }
 
    private void addFrame(Class classe) {
        boolean isCriado = true;

        // Se não houver nenhum JIF aberto ele cria um JIF
        if (jDesktopPane.getSelectedFrame() == null) {
            /* Através do simple factory eu reduzo o acoplamento do código
             *  melhorando a performance e manutenção */
            addJIF(JIFSimpleFactory.getJIF(classe));
            // Caso contrário..
        } else {
            JOptionPane.showMessageDialog(null, "Feche o controle atual e tente novamente.");
        }
        System.gc();
    }
    
    private void abrirJIFChamadoTecnico() {
        addFrame(JIFChamadoTecnico.class);
    }     
   
    private void fecharAplicacao() {
        int n = JOptionPane.showConfirmDialog(rootPane,
                "Deseja sair do sistema? ",
                "Sair do sistema",
                JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

}
